/**
 *添加设备
 */
'use strict';

const operationRecord = require("../sys/operationRecordAction");
const Transaction = require('../../libs/DB/Transaction');
const deviceTypeObj={
    "1":{
        name:"water",
        measureList:[1],
    },
    "2":{
        name:"electric",
        measureList:[2],
    },
    "3":{
        name:"cooling_tower",
        measureList:[],
    },
    "4":{
        name:"heat",
        measureList:[3,5],
    },
    "5":{
        name:"flow_meter",
        measureList:[5],
    },
    "6":{
        name:"heating_cooling_meter",
        measureList:[3,4,5],
    },
    "7":{
        name:"valve",
        measureList:[6],
    },
    "8":{
        name:"water_pump",
        measureList:[6],
    },
    "9":{
        name:"temperature",
        measureList:[],
    },
    "10":{
        name:"vav_temperature",
        measureList:[],
    },
    "11":{
        name:"sampling",
        measureList:[],
    },
    "12":{
        name:"refrigerating_machine",
        measureList:[6],
    },
}
const {client} = require("../../../server/libs/Redis/RedisClient");


module.exports = async function (params) {
    let transaction = new Transaction()
    try {
        await transaction.beginTransaction();
        let result = await transaction.query(`SELECT count(*) AS count FROM device_file WHERE device_no = "${params.deviceNo}" AND delete_sign = 1;SHOW TABLES LIKE "data_${deviceTypeObj[`${params.deviceType}`].name}_${params.userDomainId}"`);
        if(result[0][0].count > 0 ) throw {message:"当前设备编号已存在。"};
        let insertObj ={
            device_no:params.deviceNo,
            device_type:params.deviceType,
            device_name:params.deviceName,
            range_id:params.rangeId,
            enable:2,
            parent_id:params.parentId ? params.parentId : 0,
            accumulate:params.accumulate ? params.accumulate : 0,
        }
        let deviceId;
        if(result[1].length < 1){
            let insertSql = `INSERT INTO device_file SET ?;${result[1].length < 1 ? `CALL create_${deviceTypeObj[`${params.deviceType}`].name}_data_table(?)` : ``}`
            let insertDeviceResult = await transaction.query(insertSql, [insertObj,params.userDomainId]);
            deviceId = insertDeviceResult[0].insertId;
        }else{
            let insertSql = `INSERT INTO device_file SET ?`
            let insertDeviceResult = await transaction.query(insertSql, [insertObj]);
            deviceId = insertDeviceResult.insertId
        }
        if(deviceTypeObj[`${params.deviceType}`].measureList.length >0){
            let measureInsertSql = ``;
            const multi = client.multi();
            for(const measure of deviceTypeObj[`${params.deviceType}`].measureList){
                measureInsertSql += `INSERT INTO cost_plan_device_mid SET device_id = ${deviceId},measure_type = ${measure};`
                multi.hmset(`deviceInfo-${measure}-${insertObj.deviceNo}`,{deviceNo:insertObj.deviceNo,domainId:params.userDomainId,accumulate:0});
            }
            await transaction.query(measureInsertSql, [insertObj]);
            multi.exec();
        }
        await transaction.commit();
        await operationRecord(params);
        return { success: true };
    }catch (e){
        await transaction.rollback();
        return {success:false,message:e.message};
    }
};
